/**
 *
 * @param {*} records - 数组数据
 * @param {*} id - 上级id
 * @returns - 返回树形菜单chidren
 */
function getChilds(records, id) {
  const childs = []
  records.forEach((val) => {
    id === val.parentId && childs.push({ ...val })
  })
  childs.forEach((childNode) => {
    const currNode = getChilds(records, childNode.id)
    currNode.length > 0 && (childNode.children = currNode)
  })
  return childs
}
/**
 *顶层节点依据: 传入的rootId为顶层节点，数据的parentId为rootId的时候，说明该条数据是顶层节点的下一层
 * @param {Array} records - 数组数据
 * @param {string} rootId - 根节点id
 * @returns {Array} 返回结果数据
 */
export function custTree(records, rootId) {
  var result = []
  records.forEach((val) => {
    if (val.parentId === rootId) {
      const parent = { ...val }
      parent.children = getChilds(records, val.id)
      result.push(parent)
    }
  })
  return result
}
